//
//  AttrStriingGenetator.swift
//  七哥直播
//
//  Created by seven on 2017/12/29.
//  Copyright © 2017年 seven. All rights reserved.
//

import UIKit
import Kingfisher

class AttrStriingGenetator {
    
}

extension AttrStriingGenetator{
    
    class func generateJoinLeaveRoom(userName: String, isJoin: Bool) -> NSAttributedString{
        let joinRoomStr = isJoin ? "\(userName)进入房间" : "\(userName)离开房间"
        let roomAttr = NSMutableAttributedString(string: joinRoomStr)
        roomAttr.addAttributes([NSAttributedStringKey.foregroundColor: UIColor.orange], range: NSRange(location: 0, length: userName.count))
        return roomAttr
    }
    
    class func generateTextMessage(userName: String, message: String) -> NSAttributedString{
        //1.获取整个字符串
        let chatMessage = "\(userName): \(message)"
        //2.根据字符串创建对应的富文本
        let chatMsgAttr = NSMutableAttributedString(string: chatMessage)
        //3.将用户名改成橘色
        chatMsgAttr.addAttributes([NSAttributedStringKey.foregroundColor: UIColor.orange], range: NSRange(location: 0, length: userName.count))
        //4.将所有表情匹配粗来，并且换成对应的图片进行展示
        //4.1创建正则表达式匹配表情
        let pattern = "\\[.*?\\]"
        guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {return chatMsgAttr}
        let resultArr = regex.matches(in: chatMessage, options: [], range: NSRange(location: 0, length: chatMessage.count))
        //4.2获取表情遍历结果
        for i in (0..<resultArr.count).reversed() {
            let result = resultArr[i]
            let emoticonNamne = (chatMessage as NSString).substring(with: result.range)
            //4.4根据结果创建对应的图片
            guard  let image = UIImage(named: emoticonNamne) else {continue}
            //4.5根据土坯那创建NSAttachment
            let attachment = NSTextAttachment()
            attachment.image = image
            let font = UIFont.systemFont(ofSize: 15)
            //4.5修改attachment大小
            attachment.bounds = CGRect(x: 0, y: -3, width: font.lineHeight, height: font.lineHeight)
            let imageAttr = NSAttributedString(attachment: attachment)
            //4.6将imageAttr替换到之前文本的位置
            chatMsgAttr.replaceCharacters(in: result.range, with: imageAttr)
            //4.7将文本属性字符串插入到内容
            //            chatMsgAttr.append(imageAttr)
        }
        return chatMsgAttr
    }
    
    class func generateGiftMessage(giftname: String, giftURL: String, userNmae: String) -> NSAttributedString{
        //1.获取赠送礼物字符串
        let sendGiftMsg = "\(userNmae) 赠送 \(giftname)"
        //2.根据字符串创建NSMutableAttributedString
        let sendGiftMAttrMsg = NSMutableAttributedString(string: sendGiftMsg)
        //3.修改用户名称
        sendGiftMAttrMsg.addAttributes([NSAttributedStringKey.foregroundColor: UIColor.orange], range: NSRange(location: 0, length: userNmae.count))
        //4.修改礼物的名称
        let range = (sendGiftMsg as NSString).range(of: giftname)
        sendGiftMAttrMsg.addAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], range: range)
        //5.在最后拼接上礼物的图片
        //        guard let giftURL = URL(string: giftMsg.giftUrl) else {return}
        guard let image = KingfisherManager.shared.cache.retrieveImageInMemoryCache(forKey: giftURL) else {return sendGiftMAttrMsg}
        let attachment = NSTextAttachment()
        attachment.image = image
        let font = UIFont.systemFont(ofSize: 15)
        //4.5修改attachment大小
        attachment.bounds = CGRect(x: 0, y: -3, width: font.lineHeight, height: font.lineHeight)
        //4.6将attachment添加到最后
        let imageAttr = NSAttributedString(attachment: attachment)
        sendGiftMAttrMsg.append(imageAttr)
//        //5将内容插入到chatContent中
//        chatContentView.insertMsg(sendGiftMAttrMsg)
        return sendGiftMAttrMsg
    }
}
